package com.tfswx.jkgl.core.api;

import cn.hutool.core.bean.BeanUtil;
import com.tfswx.jkgl.core.bean.JkglRuntimeData;
import com.tfswx.jkgl.core.config.JkglDataConfig;
import com.tfswx.jkgl.core.consts.Const;
import com.tfswx.jkgl.core.dto.jkgl.JkDataUploadInputDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * @author ricky
 * @date 2021/12/15
 */
@Api(tags = "接口管理")
@RestController
@RequestMapping("jkgl")
public class JksjglController {
    @Resource
    private JkglRuntimeData jkglRuntimeData;
    @Resource
    private JkglDataConfig jkglDataConfig;

    @ApiOperation("上传接口数据")
    @PostMapping("uploadJkData")
    public Boolean uploadJkData(@Validated JkDataUploadInputDTO inputDTO) {
        try {
            MultipartFile file = inputDTO.getFile();
            if (file.isEmpty()) {
                throw new RuntimeException("数据包上传失败");
            }
            File file1 = new File(Const.jkConfonfigPath);
            file.transferTo(file1.getCanonicalFile());
            JkglRuntimeData jkglRuntimeData = jkglDataConfig.loadJkglData();
            BeanUtil.copyProperties(jkglRuntimeData, this.jkglRuntimeData);
            return true;
        } catch (IOException e) {
            throw new RuntimeException("数据包上传失败", e);
        }
    }
}
